#include "kdcstatusbar.h"

#include <QTimer>
#include <KLocale>
#include <kio/job.h>
#include <QWidget>
#include <QHBoxLayout>
#include <QPalette>
#include <KDiskFreeSpace>
#include <kdclib.h>
#include <QFrame>
#include <KIcon>

KDCStatusBar::KDCStatusBar( QWidget *aParent ) : KStatusBar( aParent ), pOutMountPoint("/"), pTempMountPoint("/"), pProjectSize( 0 )
{
	pInfoLabel = new InfoLabel( this );
	insertWidget( 0, pInfoLabel, 13 );
	
	pDiscSizeBar = new QProgressBar( this );
	pDiscSizeBar->setMinimumHeight( 20 );
	pDiscSizeBar->setMaximumHeight( 20 );
	insertWidget( 1, pDiscSizeBar, 10 );
	
	pDelOryginalFilesLabel = new QLabel( i18n("DEL"), this );
	pDelOryginalFilesLabel->setToolTip( i18n("Delete oryginal files") );
	pDelOryginalFilesLabel->setFrameShape( QFrame::Box );
	pDelOryginalFilesLabel->setFrameShadow( QFrame::Sunken );
	pDeleteTempFilesLabel = new QLabel( i18n("TEMP"), this );
	pDeleteTempFilesLabel->setToolTip( i18n("Delete temporary files") );
	pDeleteTempFilesLabel->setFrameShape( QFrame::Box );
	pDeleteTempFilesLabel->setFrameShadow( QFrame::Sunken );
	pCreateStructureLabel = new QLabel( i18n("STR"), this );
	pCreateStructureLabel->setToolTip( i18n("Create disc structure") );
	pCreateStructureLabel->setFrameShape( QFrame::Box );
	pCreateStructureLabel->setFrameShadow( QFrame::Sunken );
	QWidget *tmp = new QWidget( this );
	QHBoxLayout *hLayout = new QHBoxLayout( tmp	);
	hLayout->addWidget( pDelOryginalFilesLabel );
	hLayout->addWidget( pDeleteTempFilesLabel );
	hLayout->addWidget( pCreateStructureLabel );
	insertWidget( 2, tmp, 1 );

	pOutFolderInfo = new QLabel( i18n( "Output:" ) + " 0.0 B / 0.0 B", this );
	pOutFolderIcon = new QLabel( this );
	pOutFolderIcon->setPixmap( KIcon("folder-green").pixmap( 20, 20 ) );
	pTempFolderInfo = new QLabel( i18n( "Temp:" ) + " 0.0 B / 0.0 B", this );
	pTempFolderIcon = new QLabel( this );
	pTempFolderIcon->setPixmap( KIcon("folder-green").pixmap( 20, 20 ) );
	QFrame *frame = new QFrame( this );
	hLayout = new QHBoxLayout( frame );
	hLayout->addWidget( pTempFolderIcon );
	hLayout->addWidget( pTempFolderInfo );
	frame->setFrameShape( QFrame::Box );
	frame->setFrameShadow( QFrame::Sunken );
	frame->setMaximumHeight( 20 );
	insertWidget( 3, frame, 4 );
	
	frame = new QFrame( this );
	hLayout = new QHBoxLayout( frame );
	hLayout->addWidget( pOutFolderIcon );
	hLayout->addWidget( pOutFolderInfo );
	frame->setFrameShape( QFrame::Box );
	frame->setFrameShadow( QFrame::Sunken );
	frame->setMaximumHeight( 20 );
	insertWidget( 4, frame, 4 );

	QLabel *ver = new QLabel( "0.4", this );
	ver->setFrameShape( QFrame::Box );
	ver->setFrameShadow( QFrame::Sunken );
	ver->setMaximumHeight( 20 );
	insertWidget( 5, ver, 1 );
	
	clear();
	
	refreshFreeSpace();
	pTimer = new QBasicTimer;
	pTimer->start( 30000, this );
}

KDCStatusBar::~KDCStatusBar()
{
	pTimer->stop();
	deletePtr( pTimer );
}

void KDCStatusBar::showMessage( const QString &aText, KDC::MessageType aType )
{
	QString icon = "dialog-error";
	if ( aType == KDC::Info )
		icon = "dialog-information";
	else
		if ( aType == KDC::Warning )
			icon = "dialog-warning";
	pInfoLabel->setMessage( aText, KIcon(icon) );
	QTimer::singleShot( 4000, pInfoLabel, SLOT( clear() ) );
}

void KDCStatusBar::setProjectSize( quint64 aSize, KDC::MediaSize aMediaSize )
{
	pProjectSize = aSize;
	pDiscSizeBar->setMaximum( aMediaSize );
	if ( aSize > aMediaSize )
	{
		int percent = qRound( aSize/(double)aMediaSize*100 );
		pDiscSizeBar->setFormat( KIO::convertSizeFromKiB( aSize ) + " / " + QString::number( percent ) + "%" );
		pDiscSizeBar->setValue( aMediaSize );
	}
	else
	{
		pDiscSizeBar->setFormat( KIO::convertSizeFromKiB( aSize ) + " / %p%"  );
		pDiscSizeBar->setValue( aSize );
	}
}

void KDCStatusBar::setProjectOutputPath( const QString &aPath )
{
	pOutMountPoint = mountPoint( aPath );
	pOutFolderInfo->setToolTip( aPath );
	pOutFolderIcon->setToolTip( aPath );
}

void KDCStatusBar::setProjectTempPath( const QString &aPath )
{
	pTempMountPoint = mountPoint( aPath );
	pTempFolderInfo->setToolTip( aPath );
	pTempFolderIcon->setToolTip( aPath );
}

void KDCStatusBar::setDeleteInputFilesEnabled( bool aEnabled )
{
	drawLabel( pDelOryginalFilesLabel, aEnabled );
}

void KDCStatusBar::setDeleteTempFilesEnabled( bool aEnabled )
{
	drawLabel( pDeleteTempFilesLabel, aEnabled );
}

void KDCStatusBar::setCreateDiscStructureEnabled( bool aEnabled )
{
	drawLabel( pCreateStructureLabel, aEnabled );
}

void KDCStatusBar::clear()
{
	pInfoLabel->clear();
	pDiscSizeBar->setFormat( i18n("0.0 B  / %p%") );
	pDiscSizeBar->setValue( 0 );
	setDeleteInputFilesEnabled( false );
	setDeleteTempFilesEnabled( true );
	setCreateDiscStructureEnabled( true );
	pOutFolderIcon->setPixmap( KIcon("folder-green").pixmap( 20, 20 ) );
	pOutFolderInfo->setText( i18n( "Output:" ) + " 0.0 B / 0.0 B" );
	pTempFolderIcon->setPixmap( KIcon("folder-green").pixmap( 20, 20 ) );
	pTempFolderInfo->setText( i18n( "Temp:" ) + " 0.0 B / 0.0 B" );
	pProjectSize = 0;
}

void KDCStatusBar::drawLabel( QLabel *aLabel, bool aEnabled )
{
	QPalette p;
	if ( aEnabled )
		p.setColor( QPalette::Active, QPalette::WindowText, p.color( QPalette::Active, QPalette::WindowText ) );
	else
		p.setColor( QPalette::Active, QPalette::WindowText, p.color( QPalette::Disabled, QPalette::WindowText ) );
	aLabel->setPalette( p );
}

void KDCStatusBar::refresh()
{
	refreshFreeSpace();
}

void KDCStatusBar::refreshFreeSpace()
{
	KDiskFreeSpace *freeDiskSpace = new KDiskFreeSpace( this );
	connect( freeDiskSpace, SIGNAL( foundMountPoint( const QString&, quint64, quint64, quint64 ) ), this, SLOT( slotFoundOutMountPoint( const QString&, quint64, quint64, quint64 ) ) );
	freeDiskSpace->readDF( pOutMountPoint );
	
	freeDiskSpace = new KDiskFreeSpace( this );
	connect( freeDiskSpace, SIGNAL( foundMountPoint( const QString&, quint64, quint64, quint64 ) ), this, SLOT( slotFoundTempMountPoint( const QString&, quint64, quint64, quint64 ) ) );
	freeDiskSpace->readDF( pTempMountPoint );
}

void KDCStatusBar::slotFoundOutMountPoint( const QString &aMountPoint, quint64 aKBSize, quint64 aKBUsed, quint64 aKBAvail )
{
	quint64 freeSize = aKBAvail*1024;
	quint64 totalSize = aKBSize*1024;
	pOutMountPoint = aMountPoint;

	if ( pProjectSize > freeSize )
		pOutFolderIcon->setPixmap( KIcon("folder-red").pixmap( 20, 20 ) );
	else
		if ( ( pProjectSize + 52428800 ) > freeSize )
			pOutFolderIcon->setPixmap( KIcon("folder-orange").pixmap( 20, 20 ) );
	else
		pOutFolderIcon->setPixmap( KIcon("folder-green").pixmap( 20, 20 ) );

	pOutFolderInfo->setText( i18n("Output: ") + KIO::convertSize( freeSize ) + " / " + KIO::convertSize( totalSize ) );
}

void KDCStatusBar::slotFoundTempMountPoint( const QString &aMountPoint, quint64 aKBSize, quint64 aKBUsed, quint64 aKBAvail )
{
	quint64 freeSize = aKBAvail*1024;
	quint64 totalSize = aKBSize*1024;
	pTempMountPoint = aMountPoint;

	if ( pProjectSize > freeSize )
		pTempFolderIcon->setPixmap( KIcon("folder-red").pixmap( 20, 20 ) );
	else
		if ( ( pProjectSize + 52428800 ) > freeSize )
			pTempFolderIcon->setPixmap( KIcon("folder-orange").pixmap( 20, 20 ) );
	else
		pTempFolderIcon->setPixmap( KIcon("folder-green").pixmap( 20, 20 ) );

	pTempFolderInfo->setText( i18n("Temp: ") + KIO::convertSize( freeSize ) + " / " + KIO::convertSize( totalSize ) );
}

void KDCStatusBar::timerEvent( QTimerEvent *aEvent )
{
	if ( aEvent->timerId() == pTimer->timerId() )
		refreshFreeSpace();
	else
		QWidget::timerEvent( aEvent );
}
